<?php 
include('../common/_const.php');
include('../common/common.php');
include('../common/checksession.php');
checksession("../index.php");
include('../../inc/conn.php');
include('../../inc/website_info.php');
date_default_timezone_set('Asia/Chongqing');//设置时区 

//获取变量
$channelid	=1;
$aid		=$_POST['arcid'];
$title		=exquot(excode($_POST['article_name']));
$flag		=$_POST['flags'];
$source		=exquot(excode($_POST['source']));
$auther		=exquot(excode($_POST['auther']));
$typeid		=$_POST['typeid'];
$keywords	=isempty(exquot(excode($_POST['keywords'])));
$description=isempty(exquot(excode($_POST['description'])));
$feedback	=$_POST['feedback'];
$click		=$_POST['click'];
$pubdate	=$_POST['insert_time']." ".date("H:i:s");
$ishidden	=$_POST['ishidden'];
$arc_content=stripslashes($_POST['arc_content']);
$litpic		=$_FILES['litpic'];
$userip		="192.168.1.1";
$gurl		=$_POST['gurl'];


//检测时间格式
if(!preg_match("/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/",$pubdate)){ 
	ShowMsg("时间格式错误!",$gurl);
	exit;
}

//后台数据校验
if($aid){
	isnum($aid,"文档修改失败!",$gurl);
}else{
	ShowMsg("文档修改失败!",$gurl);
}

isnum($click,"浏览次数格式错误!",$gurl);
isnum($typeid,"系统错误，请重新填写!",$gurl);
isnum($feedback,"系统错误，请重新填写!",$gurl);
isnum($ishidden,"系统错误，请重新填写!",$gurl);
if($title==""||$arc_content==""){
	ShowMsg("数据填写不完整!",$gurl);
	exit;
}
//把pubdate转换成时间戳
$year=(int)(substr($pubdate,0,4));
$month=(int)(substr($pubdate,5,2));
$day=(int)(substr($pubdate,8,2));
$hour=(int)(substr($pubdate,11,2));
$minutes=(int)(substr($pubdate,14,2));
$second=(int)(substr($pubdate,17,2));
$pubdate=mktime($hour,$minutes,$second,$month,$day,$year);

if($flag){
$flag=implode(",",$flag);
}

//定义允许上传的文件扩展名
$ext_arr = array('gif', 'jpg', 'jpeg', 'png');

//定义缩略图路径
$litpic_src="";

//是否有缩略图
if($litpic['name']){

	if($litpic["error"]==0){
		$dirpath="../../attached/allimg/";
		$dirurl= $web_info['website_root']."attached/allimg/";
		$sourcename=$litpic['name'];//源文件名
		$base=date("YmdHis").mt_rand();//基本名，加上一个序号，防止覆盖
		$ext=getExtension($sourcename);//扩展名
		$filename=$base.".".$ext;//新文件名
		$max_size=614400;
	
		//检查目录
		if (@is_dir($dirpath) === false) {
			ShowMsg("上传目录出错!",$gurl);
			exit;
		}
		//检查目录写权限
		if (@is_writable($dirpath) === false) {
			ShowMsg("上传目录不可写!",$gurl);
			exit;
		}
		//检查是否已上传
		if (@is_uploaded_file($litpic['tmp_name']) === false) {
			ShowMsg("上传失败!",$gurl);
			exit;
		}
		//检查文件大小
		if ($litpic['size'] > $max_size) {
			ShowMsg("上传图片不能大于600K!",$gurl);
			exit;
		}
		//检查扩展名
		if (in_array($ext, $ext_arr) === false) {
			ShowMsg("上传文件类型不被允许!",$gurl);
			exit;
		}
		
		if(move_uploaded_file($litpic['tmp_name'],$dirpath.$filename)===false){
			ShowMsg("上传失败,请重新上传!",$gurl);
			exit;
		}
		@chmod($dirpath.$filename, 0644);
		$litpic_path=$dirpath.$filename;//图片相对路径
		$litpic_src=$dirurl.$filename;//图片绝对路径
		
	}else{
		ShowMsg("上传失败,请重新上传!",$gurl);
		exit;
	}

}

//生成缩略图
if($litpic_src){
	$source_img=$litpic_path;
	$img_info=getimagesize($source_img);
	
	switch ($img_info[2])
	{
		case 1:$img=imagecreatefromgif($source_img);break;
		case 2:$img=imagecreatefromjpeg($source_img);break;
		case 3:$img=imagecreatefrompng($source_img);break;
	}
	
	if($web_info['website_sy']){//判断是否创建水印
	//创建文字水印
	$font_height=(int)($img_info[1]/20);
	$txt_bg=@imagecolorallocate($img,255,0,55) or die("pic info error");
	$txt_str=$web_info['website_sytxt'];
	imagettftext($img,$font_height,0,20,(int)(400/$img_info[0]*$img_info[1]),$txt_bg,'../common/data/simhei.ttf',$txt_str);
	}
	
	//输出缩略图
	$lit_width=400;
	$lit_height=(int)($lit_width/$img_info[0]*$img_info[1]);
	$lit_img=imagecreatetruecolor($lit_width,$lit_height);
	imagecopyresized($lit_img,$img,0,0,0,0,$lit_width,$lit_height,$img_info[0],$img_info[1]);
	
	switch ($img_info[2])
	{
		case 1:imagegif($lit_img,$dirpath.$base.".litpic.".$ext);break;
		case 2:imagejpeg($lit_img,$dirpath.$base.".litpic.".$ext);break;
		case 3:imagepng($lit_img,$dirpath.$base.".litpic.".$ext);break;
	}

	$litpic_src=$dirurl.$base.".litpic.".$ext;
	
}

$db=db_connect();//连接数据库
$db->query('SET AUTOCOMMIT=0'); // 设置为不自动提交查询 
$db->query('START TRANSACTION'); // 开始查询，这里也可以使用BEG

$sql_main=update_sql("`".PRETABLE."arcmain`","`title`='".$title."',`typeid`='".$typeid."',`flag`='".$flag."',`write`='".$auther."',`source`='".$source."',`pubdate`='".$pubdate."',`description`='".$description."',`keywords`='".$keywords."',`isguest`='".$feedback."',`click`='".$click."',`ishidden`='".$ishidden."'","`id`='".$aid."'");
$sql_arc=update_sql("`".PRETABLE."article`","`typeid`='".$typeid."',`body`='".$arc_content."',`userip`='".$userip."'","`aid`='".$aid."'");

if($litpic_src){
$sql_litpic=update_sql("`".PRETABLE."arcmain`","`litpic`='".$litpic_src."'","`id`='".$aid."'");
if(!$db->query($sql_litpic)){
	ShowMsg("文章修改失败,请重新修改!",$gurl);
	exit;
	}
}
$db->query($sql_main);
$db->query($sql_arc);

if($db->affected_rows<0){
	$db->query('ROLLBACK');//如果遇到错误数据回滚
	ShowMsg("文章修改失败,请重新修改!",$gurl);	
	$db->query('COMMIT');
	$db->close();
	exit;
}
	$db->query('COMMIT');
	$db->close();

ShowMsg_h("成功修改文章!","../moduls/content_list.php?catid=".$typeid."&pages=1");	
?>